/*	$Id: dlink.c 7 2008-07-29 02:58:11Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <stdlib.h>
#include <string.h>

#include "if.h"
#include "dlink.h"
#include "pforge.h"


#define PFDL_RBUF_DEFAULT   2048


#ifdef DLINK_BPF
extern struct pf_dlink_ops dlink_bpf_ops;
#endif

#ifdef DLINK_DLPI
extern struct pf_dlink_ops dlink_dlpi_ops;
#endif

#ifdef DLINK_LPKT
extern struct pf_dlink_ops dlink_lpkt_ops;
#endif

#ifdef DLINK_FILE
extern struct pf_dlink_ops dlink_file_ops;
#endif

#ifdef DLINK_PCAP
extern struct pf_dlink_ops dlink_pcap_ops;
#endif


/* dl operations switch table */
static struct pf_dlink_ops *dlops_swtab[] = {
	NULL,
#ifdef DLINK_BPF
	&dlink_bpf_ops,
#else
	NULL,
#endif
#ifdef DLINK_DLPI
	&dlink_dlpi_ops,
#else
	NULL,
#endif
#ifdef DLINK_LPKT
	&dlink_lpkt_ops,
#else
	NULL,
#endif
#ifdef DLINK_PCAP
	&dlink_pcap_ops,
#else
	NULL,
#endif
#ifdef DLINK_FILE
	&dlink_file_ops,
#else
	NULL,
#endif
};

#define NB_DLOPS (sizeof(dlops_swtab)/sizeof(struct pf_dlink_ops *))


/*
 * pf_dlink_init()
 *
 * Initialize the datalink interface layer.
 */
int
pf_dlink_init(void)
{
	/* nothing to do */
	return (0);
}


/*
 * pf_dlink_open()
 *
 * Open a datalink thingie of type <type>
 *
 * Returns a pointer to the opened datalink object on success, or NULL
 * on failure.
 */
PF_DLINK*
pf_dlink_open(int type)
{
	unsigned int i;
	PF_DLINK *dl;

	if (type != PFDL_TYPE_ANY &&
	    (type >= (int)NB_DLOPS || dlops_swtab[type] == NULL)) {
		pf_seterr(PF_ERR_INVARG);
		return (NULL);
	}

	if ((dl = (PF_DLINK *)pf_malloc(sizeof(*dl))) == NULL)
		return (NULL);
	memset(dl, 0, sizeof(*dl));

	/* by default, use an infinite timeout, for small values of infinity */
	dl->timeout.tv_sec = 0xffff;
	dl->timeout.tv_usec = 0;

	if (type == PFDL_TYPE_ANY) {
		/* pick the first available type in the switch table */
		for (i = 0; i < NB_DLOPS; i++)
			if (dlops_swtab[i] != NULL) {
				type = i;
				break;
			}
	}
	if (type == PFDL_TYPE_ANY) {
		pf_free(dl);
		pf_seterr(PF_ERR_NOTSUP);
		return (NULL);
	}

	/* preallocate a read buffer of appropriate size */
	if ((dl->rbuf = pf_malloc(PFDL_RBUF_DEFAULT)) == NULL) {
		pf_free(dl);
		return (NULL);
	}
	dl->rbufsz = PFDL_RBUF_DEFAULT;

	/* bound checked type earlier */
	if (dlops_swtab[type]->dlk_open(dl) < 0) {
		pf_free(dl->rbuf);
		pf_free(dl);
		return (NULL);
	}
	dl->dltype = type;
	return (dl);
}


/*
 * pf_dlink_close()
 *
 * Close the datalink interface bound to <dl>.
 * Returns 0 on success, or -1 on failure.
 */
int
pf_dlink_close(PF_DLINK *dl)
{
	int ret;

	ret = dlops_swtab[dl->dltype]->dlk_close(dl);
	if (dl->rbuf != NULL)
		pf_free(dl->rbuf);
	pf_free(dl);

	return (ret);
}

/*
 * pf_dlink_bind()
 *
 * Bind the datalink interface <dl> to the interface whose index is given in
 * <nid>.
 * Returns 0 on success, or -1 on failure.
 */
int
pf_dlink_bind(PF_DLINK *dl, int nid)
{
	int ret;

	if (dl->dlflags & PFDL_BOUND) {
		return (-1);
	}

	ret = dlops_swtab[dl->dltype]->dlk_bind(dl, nid);
	if (ret >= 0) {
		dl->dlflags |= PFDL_BOUND;
	}

	return (ret);
}


/*
 * pf_dlink_ctl()
 *
 * Control various parameters of the datalink interface <dl>.  The type of
 * operation is given in <ctl> and optional data is specified in <data>.
 * Returns 0 on success, or -1 on failure.
 */
int
pf_dlink_ctl(PF_DLINK *dl, int ctl, void *data)
{
	int ret;
	struct pf_dlink_stats *stp;

	if (dlops_swtab[dl->dltype] == NULL)
		return (-1);

	/* generic control operations */
	switch (ctl) {
	case PFDL_CTL_GSTATS:
		stp = (struct pf_dlink_stats *)data;
		memcpy(stp, &(dl->dl_stats), sizeof(dl->dl_stats));
		break;
	default:
		/* dispatch to datalink-specific layer */
		ret = dlops_swtab[dl->dltype]->dlk_ctl(dl, ctl, data);
		break;
	}

}


/*
 * pf_dlink_read()
 *
 * Read data from the underlying interface and store it into <dst>, which is
 * of size <len>.
 * Returns the number of bytes read, or -1 on error.
 */
PF_PKT*
pf_dlink_read(PF_DLINK *dl)
{
	if (!(dl->dlflags & PFDL_BOUND)) {
		return (NULL);
	}
	return dlops_swtab[dl->dltype]->dlk_read(dl);
}


/*
 * pf_dlink_write()
 *
 * Write <len> bytes of data to the datalink interface <dl> and starting at
 * address <dst>.
 * Returns the number of bytes actually written, or -1 on error.
 */
ssize_t
pf_dlink_write(PF_DLINK *dl, void *dst, size_t len)
{
	if (!(dl->dlflags & PFDL_BOUND)) {
		return (-1);
	}
	return dlops_swtab[dl->dltype]->dlk_write(dl, dst, len);
}
